package com.shoujifeng.snowmusic.player.utils;

import android.app.Activity;
import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.shoujifeng.snowmusic.player.R;

public class LoadMask {
	private Context mContext;
	private View mLoadView;
	private View mErrorView;
	private boolean mbShow = false;

	public LoadMask(Context context) {
		mContext = context;
		mLoadView = null;
		mErrorView = null;
	}

	public void startLoad() {
		if (mLoadView == null) {
			LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
					LinearLayout.LayoutParams.FILL_PARENT,
					LinearLayout.LayoutParams.FILL_PARENT);

			params.topMargin = 0;
			params.gravity = Gravity.TOP | Gravity.CENTER_HORIZONTAL;

			mLoadView = LayoutInflater.from(mContext).inflate(
					R.layout.loading_layout, null);

			((Activity) mContext).addContentView(mLoadView, params);

		} else {
			mLoadView.setVisibility(View.VISIBLE);
		}

		mbShow = true;
	}
	
	public void startLoad(String title) {
		if (mLoadView == null) {
			LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
					LinearLayout.LayoutParams.FILL_PARENT,
					LinearLayout.LayoutParams.FILL_PARENT);

			params.topMargin = 0;
			params.gravity = Gravity.TOP | Gravity.CENTER_HORIZONTAL;

			mLoadView = LayoutInflater.from(mContext).inflate(
					R.layout.loading_layout, null);
			
			TextView titleTx =(TextView) mLoadView.findViewById(R.id.load_title);
			titleTx.setText(title);

			((Activity) mContext).addContentView(mLoadView, params);

		} else {
			mLoadView.setVisibility(View.VISIBLE);
		}

		mbShow = true;
	}

	public boolean stopLoad() {

		boolean bRed = true;
		if (mLoadView != null && mbShow) {
			mLoadView.setVisibility(View.GONE);
		} else {
			bRed = false;
		}

		mbShow = false;

		return bRed;
	}

	public void showError(String title) {

		LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
				LinearLayout.LayoutParams.FILL_PARENT,
				LinearLayout.LayoutParams.FILL_PARENT);

		params.topMargin = 0;
		params.gravity = Gravity.TOP | Gravity.CENTER_HORIZONTAL;

		mErrorView = LayoutInflater.from(mContext).inflate(
				R.layout.notnet_layout, null);
		
		TextView text = (TextView)mErrorView.findViewById(R.id.notnet_title);
		text.setText(title);

		((Activity) mContext).addContentView(mErrorView, params);

	}
}
